package org.example.operation;
import java.util.*;
import java.time.*;
import org.example.model.*;
import org.example.trans.*;
import org.example.inout.*;

/**
 * @author lenovo
 */

public class DataOperation {
    //生成随机num
    public static String randomNum(){
        ScoreOperation.loadScore();
        HashMap<String,Score> hashMap = Save.getHashMapScore();
        Random random = new Random();
        String randomNum;
        if(hashMap.size() >= 800000) {
            return null;
        }else{
            do{
                randomNum = String.format("%06d", random.nextInt(1000000));
            }while(hashMap.containsKey(randomNum));
        }
        return randomNum;
    }
    //生成随机name
    public static String randomName(){
        Random random = new Random();
        return Save.getLastNames()[random.nextInt(Save.getLastNames().length)]
                + Save.getFirstNames()[random.nextInt(Save.getFirstNames().length)]
                + Save.getFirstNames()[random.nextInt(Save.getFirstNames().length)];
    }
    //生成随机gender
    public static String randomGender(){
        Random random = new Random();
        return random.nextBoolean() ? "男" : "女";
    }
    //生成随机birthday
    public static String randomBirthday(){
        Random random = new Random();
        int year = 1991 + random.nextInt(11);
        int dayOfYear = random.nextInt(LocalDate.ofYearDay(year,1).lengthOfYear())+1;
        LocalDate localDate = LocalDate.ofYearDay(year, dayOfYear);
        return year + "." + String.format("%02d",localDate.getMonthValue()) + "."
                + String.format("%02d",localDate.getDayOfMonth());
    }
    //生成随机score
    public static double randomScore(){
        Random random = new Random();
        double score  = random.nextGaussian() * 10 + 80;
        if(score < 0){
            score = 0;
        }else if (score > 100){
            score = 100;
        }
        return score;
    }
    //检测arrayListOut每一行的列数，返回第一个与第一行的列数不同的行下标，都相同返回-1
    public static int formatValue(ArrayList<ArrayList<String>> arrayListOut){
        for(int i = 0 ; i < arrayListOut.size() ; i++){
            ArrayList<String> arrayListIn = arrayListOut.get(i);
            if(arrayListIn.size() != arrayListOut.get(0).size()){
                return i;
            }
            for(String string : arrayListIn){
                if(string == null || string.isEmpty() || string.matches(".*\\s+.*")){
                    return i;
                }
            }
        }
        return -1;
    }
    //将hashMapCourse与hashMapScore内容加入HashMap并写入文件，若超界返回-1，否则返回0
    public static int addData(HashMap<String,Course> hashMapCourse,
                              HashMap<String,Score> hashMapScore){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Course> hashMapBeforeCourse = Save.getHashMapCourse();
        HashMap<String,Score> hashMapBeforeScore = Save.getHashMapScore();
        hashMapBeforeCourse.putAll(hashMapCourse);
        hashMapBeforeScore.putAll(hashMapScore);
        if(hashMapBeforeScore.size() > 800000){
            return -1;
        }
        Save.setHashMapCourse(hashMapBeforeCourse);
        Save.setHashMapScore(hashMapBeforeScore);
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
        return 0;
    }
    //生成100000个随机内容加入HashMap并写入文件，若超界返回-1，否则返回0
    public static int addTest(){
        CourseOperation.loadCourse();
        ScoreOperation.loadScore();
        HashMap<String,Course> hashMapCourse = Save.getHashMapCourse();
        HashMap<String,Score> hashMapScore = Save.getHashMapScore();
        if(hashMapScore.size() > 700000){
            return -1;
        }
        Course courseMath = new Course();
        Course courseJava = new Course();
        Course coursePe = new Course();
        courseMath.setCourseName("数学");
        courseJava.setCourseName("Java");
        coursePe.setCourseName("体育");
        hashMapCourse.put(courseMath.getCourseName(),courseMath);
        hashMapCourse.put(courseJava.getCourseName(),courseJava);
        hashMapCourse.put(coursePe.getCourseName(),coursePe);
        Save.setHashMapCourse(hashMapCourse);
        for(int i = 0 ; i < 100000 ; i++){
            HashMap<String,CourseScore> courseScore = new HashMap<>();
            Student student = new Student();
            Score score = new Score();
            Course math = new Course();
            Course java = new Course();
            Course pe = new Course();
            CourseScore courseScoreMath = new CourseScore();
            CourseScore courseScoreJava = new CourseScore();
            CourseScore courseScorePe = new CourseScore();
            student.setNum(randomNum());
            student.setName(randomName());
            student.setGender(randomGender());
            student.setBirthday(randomBirthday());
            math.setCourseName("数学");
            java.setCourseName("Java");
            pe.setCourseName("体育");
            courseScoreMath.setCourse(math);
            courseScoreMath.setScore(randomScore());
            courseScoreJava.setCourse(java);
            courseScoreJava.setScore(randomScore());
            courseScorePe.setCourse(pe);
            courseScorePe.setScore(randomScore());
            courseScore.put(math.getCourseName(),courseScoreMath);
            courseScore.put(java.getCourseName(),courseScoreJava);
            courseScore.put(pe.getCourseName(),courseScorePe);
            score.setStudent(student);
            score.setScore(courseScore);
            hashMapScore.put(student.getNum(),score);
            Save.setHashMapScore(hashMapScore);
        }
        InOut.write(TransScore.transScoreWrite(Save.getHashMapCourse(),
                Save.getHashMapScore()),"saveScore.txt");
        return 0;
    }
}
